package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.entity.User;
import com.zzyl.mapper.UserMapper;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //查询用户
        User user = userMapper.selectByUsername(username);
        //如果用户为空，或者用户已经被禁用，则提示用户登录失败
        if(ObjectUtil.isEmpty(user) || user.getDataState().equals("1")){
            throw new RuntimeException("用户登录失败");
        }
        //属性拷贝，转换对象
        return BeanUtil.toBean(user, UserAuth.class);
    }
}
